// pages/familydoctor/signature/signature.js
// canvas 全局配置
var context = null; // 使用 wx.createContext 获取绘图上下文 context
var isButtonDown = false;
var arrx = [];
var arry = [];
var arrz = [];
var canvasw = 0;
var canvash = 0;
var app = getApp()
wx.getSystemInfo({
  success: function(res) {
    canvasw = res.windowWidth; //设备宽度
    // canvash = res.windowWidth * 7 / 15;
    canvash = res.windowHeight
  }
});
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  cancel: function() {
    wx.navigateBack({

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 使用 wx.createContext 获取绘图上下文 context
    var that = this
    var qyimg = options.qyimg
    var t_id = options.t_id
    var sid = options.sid
    var key = options.key
    var name = options.name
    var j_id = options.j_id
    var names = options.names
    var ff_id = JSON.parse(options.ff_id)
    var money = options.money
    var teamname = options.teamname
    var hzsfz = options.hzsfz
    var ifhz = options.ifhz
    var zid = options.zid
    context = wx.createCanvasContext('canvas');
    context.beginPath()
    context.setStrokeStyle('#000000');
    context.setLineWidth(4);
    context.setLineCap('round');
    context.setLineJoin('round');
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    that.setData({
      t_id: t_id,
      sid: sid,
      key: key,
      name: name,
      j_id: j_id,
      ff_id: ff_id,
      bgc: bgc,
      qyimg: qyimg,
      names: names,
      money: money,
      teamname: teamname,
      hzsfz: hzsfz,
      ifhz: ifhz,
      zid: zid
    })
  },
  canvasIdErrorCallback: function(e) {
    console.error(e.detail.errMsg)
  },
  //开始
  canvasStart: function(event) {
    isButtonDown = true;
    arrz.push(0);
    arrx.push(event.changedTouches[0].x);
    arry.push(event.changedTouches[0].y);
    //context.moveTo(event.changedTouches[0].x, event.changedTouches[0].y);

  },
  //过程
  canvasMove: function(event) {
    if (isButtonDown) {
      arrz.push(1);
      arrx.push(event.changedTouches[0].x);
      arry.push(event.changedTouches[0].y);
    };

    for (var i = 0; i < arrx.length; i++) {
      if (arrz[i] == 0) {
        context.moveTo(arrx[i], arry[i])
      } else {
        context.lineTo(arrx[i], arry[i])
      };

    };
    context.clearRect(0, 0, canvasw, canvash);
    context.setStrokeStyle('#000000');
    context.setLineWidth(4);
    context.setLineCap('round');
    context.setLineJoin('round');
    context.stroke();
    context.draw(false);
  },
  canvasEnd: function(event) {
    isButtonDown = false;
  },
  cleardraw: function() {
    //清除画布
    arrx = [];
    arry = [];
    arrz = [];
    context.clearRect(0, 0, canvasw, canvash);
    context.draw(true);
  },
  //导出图片
  getimg: function() {
    var that = this
    //查询模板ID
    var zid = that.data.zid
    if (arrx.length == 0) {
      wx.showModal({
        title: '提示',
        content: '签名内容不能为空！',
        showCancel: false
      });
      return false;
    };

    context.draw(true, function() {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 640,
        height: 430,
        destWidth: 640,
        destHeight: 430,
        canvasId: 'canvas',
        fileType: 'jpg',
        quality: 1,
        success(res) {
          console.log(res)
          var tempFilePaths = res.tempFilePath;
          that.saveimg(tempFilePaths)
        }
      })
    })

  },
  saveimg(tempFilePaths) {
    var that = this
    var uniacid = app.siteInfo.uniacid;
    var ff_id = that.data.ff_id
    var qyimg = that.data.qyimg
    var t_id = that.data.t_id
    var sid = that.data.sid
    var key = that.data.key
    var name = that.data.name
    var j_id = that.data.j_id
    var names = that.data.names
    var money = that.data.money
    var teamname = that.data.teamname
    var hzsfz = that.data.hzsfz
    var ifhz = that.data.ifhz
    var zid = that.data.zid
    wx.showLoading({
      title: '提交中',
    })
    setTimeout(function() {
      wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
        filePath: tempFilePaths,
        name: 'upfile',
        formData: {},
        success: function(res) {
          console.log(res.data)
          var qmimg = res.data
          //保存订单
          app.util.request({
            'url': 'entry/wxapp/Office.addorders',
            data: {
              ff_id: that.data.ff_id,
              qyimg: that.data.qyimg,
              t_id: that.data.t_id,
              sid: that.data.sid,
              key: that.data.key,
              name: that.data.name,
              j_id: that.data.j_id,
              names: that.data.names,
              money: that.data.money,
              qmimg: qmimg,
              hzsfz: hzsfz,
              ifhz: ifhz,
              openid: wx.getStorageSync('openid')
            },
            success: function(res) {
              console.log(res)
              var order = res.data.ordernum
              var q_id = res.data.q_id
              wx.hideLoading()
              wx.reLaunch({
                url: '/hyb_yl/czhuanjiasubpages/pages/questends/index?t_id=' + t_id + '&name=' + name + '&keywords=' + key + '&j_id=' + j_id + '&order=' + order + '&money=' + money + '&teamname=' + teamname + '&q_id=' + q_id + '&zid=' + zid,
              })
            }
          })
        },
        fail: function(res) {
          console.log(res)
        },
      })
    }, 2000)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})